package planner.domain;

import java.util.ArrayList;
import java.util.Iterator;

public class Operator {
	
	private String name;
	private ArrayList<String> preconditions;
	private ArrayList<String> effects;
	
	public Operator(String name){
		this.name = name;		
		preconditions = new ArrayList<String>();
		effects = new ArrayList<String>();
	}
	
	public Operator(String name, ArrayList<String> preConditions){
		this.name = name;		
		preconditions = preConditions;
	}
	
	public void addPrecondition(String pc){
		preconditions.add(pc);
	}
	
	public void addPreconditions(ArrayList<String> pcs){
		preconditions.addAll(pcs);
	}
	
	public void addEffect(String e){
		effects.add(e);
	}
	
	public void addEffects(ArrayList<String> es){
		effects.addAll(es);		
	}
	
	public void printOperator(boolean debug){
		if(debug){
			System.out.println(name + ":");
			System.out.println("PC: " + preconditions);
			System.out.println(" E: " + effects);
			System.out.println();
		}
	}
	
	public ArrayList<String> getPreconditions() {
		return preconditions;
	}

	public ArrayList<String> getEffects() {
		return effects;
	}

	public String getName() {
		return name;
	}
	
	public int conditionsSolvedBy(ArrayList<String> props){
		int solved = 0;
		
		Iterator<String> iProps = props.iterator();
		
		while (iProps.hasNext()) {
			String prop = (String) iProps.next();
			if(preconditions.contains(prop))
				solved++;
		}
		
		return solved;
	}
	
}
